package cn.itsource.product.controller;

import cn.itsource.basic.util.LoginContext;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.service.IEmployeeService;
import cn.itsource.pet.dto.AcceptDto;
import cn.itsource.product.service.IProductTohandleService;
import cn.itsource.product.domain.ProductTohandle;
import cn.itsource.product.query.ProductTohandleQuery;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageList;
import cn.itsource.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/productTohandle")
public class ProductTohandleController {
    @Autowired
    public IProductTohandleService productTohandleService;
    @Autowired
    private IEmployeeService employeeService;

    /**
     * 保存和修改公用的
     * @param productTohandle  传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody ProductTohandle productTohandle){
        try {
            if( productTohandle.getId()!=null) {
                productTohandleService.update(productTohandle);
            } else {
                productTohandleService.add(productTohandle);
            }
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            productTohandleService.delete(id);
            return AjaxResult.me().success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public ProductTohandle get(@PathVariable("id")Long id)
    {
        return productTohandleService.findById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<ProductTohandle> list(){

        return productTohandleService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<ProductTohandle> json(@RequestBody ProductTohandleQuery query)
    {
        return productTohandleService.queryByPage(query);
    }

    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody AcceptDto acceptDto)
    {
        productTohandleService.accept(acceptDto);
        return AjaxResult.me().success();
    }


    /**
     * 根据msgId进行拒单
     * @param msgId
     * @param request
     * @return
     */
    @PostMapping("/reject/{msgId}")
    public AjaxResult reject(@PathVariable("msgId") Long msgId, HttpServletRequest request)
    {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeService.selectByLogininfoId(logininfo.getId());
        productTohandleService.reject(msgId, employee);
        return AjaxResult.me().success();
    }

}
